package com.example.nna.voz;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;

import org.jsoup.Connection;
import org.jsoup.Connection.Method;
import org.jsoup.Connection.Response;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import android.graphics.Bitmap;

import com.example.nna.util.UserInfo;

public class Parser1 {
	private String m_url;
	private Document doc;
	Response res;
	//static Map<String, String> cookies ;
	private ArrayList<containPage1> ListContains;
	private UserInfo mUser;
	public Parser1(String url){
		m_url = url;
		ListContains = new ArrayList<containPage1>();
		mUser = new UserInfo();

	}
    public int parser(){
		try {
			if(mUser.cookies() == null){
				Connection con1 = Jsoup.connect(m_url).timeout(60000);
				doc = con1.get();
			}else{
				doc = Jsoup.connect(m_url)
						.timeout(60000)
						.cookies(mUser.cookies())
						.get();
			}
	
			if(doc.select("td[class*=tcat]").first().text().equals("vBulletin Message")){
				return 1;
			}else{
				ListContains.clear();
				PaserExecute();
				return 0;
				
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return -1;
		}
    }
    public int Login(UserInfo user){
    	mUser = user;
    	try {
			res = Jsoup.connect("http://vozforums.com/login.php?do=login")
					  .timeout(60000)
					  .data("do","login")
					  .data("vb_login_username",mUser.User())
					  .data("cookieuser", "1")
					  .data("vb_login_password",mUser.Pass())
					  .data("s", " ")
					  .data("securitytoken", mUser.Token())
					  .data("vb_login_md5password", mUser.md5Pass())
					  .data("vb_login_md5password_utf", mUser.md5Pass())
					  .method(Method.POST)
				      .execute();
			
			if(res.cookie("vfuserid") != null){
				mUser.cookies(res.cookies());
				mUser.setLogin(true); 
				return parser();
			}else return -2;	
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return -1;
		}
		
    }
    public int Logout(){
    	String urlLogout=null;
    	urlLogout = "http://vozforums.com/login.php?do=logout&"+
    			"logouthash=" + mUser.Token();
    	try {
			res = Jsoup.connect(urlLogout)
						.timeout(60000)
						.method(Method.POST)
						.execute();
			//cookies = res.cookies();
			mUser.cookies(res.cookies());
			mUser.setLogin(false); 
			return parser();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return -1;
		}
		
    }
    public ArrayList<containPage1> getContain(){
    	return ListContains;
    }
    public containPage1 getContain(int index){
    	return ListContains.get(index);
    }
    public UserInfo mUserInfo(){
    	return mUser;
    }
    
    private void PaserExecute(){
		Element eleLogin = doc.select("a[href=private.php]").first();
		if(eleLogin != null){
			String temp = doc.select("td[class=alt2]").get(0).select("a[href*=mem]").attr("href");
			temp = temp.split("=")[1];
			mUser.setUserId(temp);
		}
		Elements eleTag1s = doc.select("td[class*=tcat]");
		Iterator<Element> iteTag1 = eleTag1s.iterator();

		while(iteTag1.hasNext()){
			Element eleTag1 = iteTag1.next();
			if(eleTag1.text().contains("Welcome to the vozForums")) continue;
			ListContains.add(new containPage1(eleTag1.text(),
											  null, 
											  null,
											  null,
											  null, 
											  eleTag1.select("[href~=forum]").attr("href"), 
											  null));
			
			Element eleTag2 = eleTag1.parent().parent().nextElementSibling();
			if(eleTag2 != null){
				Elements eleTag3s = eleTag2.select("tr");
				Iterator<Element> iteTag3 = eleTag3s.iterator();
				while(iteTag3.hasNext()){
					Element eleTag4 = iteTag3.next();
					Elements eleTag5 = eleTag4.select("td");    
					Bitmap bitmap = null;
				    if(eleTag5.size()!= 1){
				    	if(eleTag5.get(0).select("img").attr("src") != null){	
							bitmap = taskLoadImage.DownloadImage(eleTag5.get(0).select("img").attr("src"));
				    	}
   	  			    	Elements eleTag6s = eleTag5.get(2).child(0).select("div");
   	  			    	String tmp = eleTag6s.get(1).text() + "\n";
   	  			    	tmp += "by " + eleTag6s.get(2).select("a[rel]").text() + " " ;
   	  			    	tmp += eleTag6s.get(3).select("div").text();
       	   	  			ListContains.add(new containPage1(eleTag5.get(1).select("td[class*=alt1Active]").text(),
       	   	  												tmp, 
       	   	  												eleTag5.get(3).text(),
       	   	  												eleTag5.get(4).text(),
       	   	  												bitmap,
       	   	  												eleTag5.get(1).select("a[href]").attr("href"), 
       	   	  												eleTag5.get(2).select("a[href]").get(1).attr("href")));
			    	
				    }
				}
			}
		}
	
    }
}
